#include "Serialization.h"
#include <string.h>
#include <stdlib.h>

int uintArrLen(const unsigned int* source)
{
	int len = 0;
	while (source[len] != 0)
		len++;

	return len;
}

void SerializeChar(char* destination, int startByte, char source)
{
	destination[startByte] = source;
}

void SerializeInt(char* destination, int startByte, int source)
{
	*((int*)(destination + startByte)) = source;
}

void SerializeString(char* destination, int startByte, char* source)
{
	int len;
	len = strlen(source);

	memcpy(destination + startByte, source, len + 1);
}

void SerializeUIntArray(char* destination, int startByte, unsigned int* source)
{
	int len = uintArrLen(source);

	memcpy(destination + startByte, source, (len + 1) * sizeof(unsigned int));
}

char DeserializeChar(char* source, int startByte)
{
	return source[startByte];
}

int DeserializeInt(char* source, int startByte)
{
	return *((int*)(source + startByte));
}

char* DeserializeString(char* source, int startByte)
{
	int len;
	len = strlen(source + startByte);

	char* newString = malloc(len + 1);

	memcpy(newString, source + startByte, len + 1);

	return newString;
}

unsigned int* DeserializeUIntArray(char* source, int startByte)
{
	int len;
	len = uintArrLen((unsigned int*)(source + startByte));

	unsigned int* newUIntArray = malloc((len + 1) * sizeof(unsigned int));

	memcpy(newUIntArray, source + startByte, (len + 1)*sizeof(unsigned int));

	return newUIntArray;
}